package com.wmc;

import java.awt.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

import com.commonfunction.CommFunction;
import com.opensymphony.xwork2.ActionContext;
/*
 * This class will store the data from the Weighmachine operator in the database
 */
import com.opensymphony.xwork2.ActionSupport;

public class WMCFormAction extends ActionSupport{
	
	private String cagent,rstno,custname,commodity,brokername,ponum,vehicleno,address,nobags,grossweight,tareweight,netweight,sysdate,systime,userid;
	HashMap dataMap = new HashMap();
	
	public HashMap getDataMap() {
		return dataMap;
	}


	public void setDataMap(HashMap dataMap) {
		this.dataMap = dataMap;
	}


	public String getCagent() {
		return cagent;
	}


	public void setCagent(String cagent) {
		this.cagent = cagent;
	}


	public String getRstno() {
		return rstno;
	}


	public void setRstno(String rstno) {
		this.rstno = rstno;
	}


	public String getCustname() {
		return custname;
	}


	public void setCustname(String custname) {
		this.custname = custname;
	}


	public String getCommodity() {
		return commodity;
	}


	public void setCommodity(String commodity) {
		this.commodity = commodity;
	}


	public String getBrokername() {
		return brokername;
	}


	public void setBrokername(String brokername) {
		this.brokername = brokername;
	}


	public String getPonum() {
		return ponum;
	}


	public void setPonum(String ponum) {
		this.ponum = ponum;
	}


	public String getVehicleno() {
		return vehicleno;
	}


	public void setVehicleno(String vehicleno) {
		this.vehicleno = vehicleno;
	}


	public String getAddress() {
		return address;
	}


	public void setAddress(String address) {
		this.address = address;
	}


	public String getNobags() {
		return nobags;
	}


	public void setNobags(String nobags) {
		this.nobags = nobags;
	}


	public String getGrossweight() {
		return grossweight;
	}


	public void setGrossweight(String grossweight) {
		this.grossweight = grossweight;
	}


	public String getTareweight() {
		return tareweight;
	}


	public void setTareweight(String tareweight) {
		this.tareweight = tareweight;
	}


	public String getNetweight() {
		return netweight;
	}


	public void setNetweight(String netweight) {
		this.netweight = netweight;
	}


	public String getSysdate() {
		return sysdate;
	}


	public void setSysdate(String sysdate) {
		this.sysdate = sysdate;
	}


	public String getSystime() {
		return systime;
	}


	public void setSystime(String systime) {
		this.systime = systime;
	}


	public String getUserid() {
		return userid;
	}


	public void setUserid(String userid) {
		this.userid = userid;
	}


	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("Form submitted");
		System.out.println("User Id "+userid);
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-YYYY");
		SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("HH:mm:SS");
		dataMap.put("date", simpleDateFormat.format(calendar.getTime()));
		dataMap.put("time", simpleTimeFormat.format(calendar.getTime()));
		dataMap.put("barcodeKey", "123456789035");
		ActionContext.getContext().getSession().put("dataMap", dataMap);
		System.out.println("WMC Form action Data is valid");
		return "successsubmit";
		
	}
	
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		try{
		CommFunction commFunction = new CommFunction();
		if (!custname.trim().equals("")) {
			if (!commFunction.isCustomerpresent(custname) ) {
				//add error
				addActionError("Customer not present");
			}else if (!commFunction.isBrokerPresent(brokername)) {
				//add error
				addActionError("Brocker Name not present");
			}
		}else if (!commFunction.isCommitionagentPresent(cagent)) {
				//add error
				addActionError("Comition agent not present");
		}else{
			addActionError("User/Comition agent not present");
		}
		
		}catch(Exception e){
			System.out.println("Validation error with");
			e.printStackTrace();
		}
	}
}
